package Box_List;
import java.awt.Color;
import java.awt.Container;
import java.awt.EventQueue;
import java.awt.Font;

import javax.swing.JFrame;
import java.awt.FlowLayout;

import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.JButton;
import javax.swing.JLabel;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JTextField;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.event.AncestorEvent;
import javax.swing.event.AncestorListener;

public class UI2 {
	JList jlist;
	private JFrame frame;
	Integer count = 0;
	JScrollPane panel = null;
	private JTextField textField;
	String motorcycle = "";
	int clickCount = 0;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					UI2 window = new UI2();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public UI2() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame();
		frame.setBounds(100, 100, 450, 300);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		final Container con = frame.getContentPane();
		con.setLayout(null);

		textField = new JTextField("选择车型");
		textField.setBounds(230, 47, 144, 35);
		textField.addMouseListener(new MouseListener() {

			public void mouseClicked(MouseEvent e) {

			}

			public void mouseEntered(MouseEvent e) {

			}

			public void mouseExited(MouseEvent e) {

			}

			public void mousePressed(MouseEvent e) {

				if (clickCount % 2 == 0) {
					panel.setVisible(true);
					jlist.setVisible(true);
				} else {
					panel.setVisible(false);
					jlist.setVisible(false);
				}
				clickCount++;
			}

			public void mouseReleased(MouseEvent e) {
			}
		});
		String[] dd = { "长城", "故宫", "天安门", "九寨沟", "张卜村", "2", "长城", "故宫",
				"天安门", "九寨沟", "张卜村", "2", "长城", "故宫", "天安门", "九寨沟", "张卜村", "2" };
		jlist = new JList(dd);
		jlist.addMouseListener(new MouseListener() {
			public void mouseClicked(MouseEvent e) {

			}

			public void mouseEntered(MouseEvent e) {
			}

			public void mouseExited(MouseEvent e) {

			}

			public void mousePressed(MouseEvent e) {

				motorcycle = "";
				Object[] arr = jlist.getSelectedValues();
				for (Object v : arr) {
					String value = v.toString().trim() + ";";
					if (!motorcycle.contains(value)) {
						motorcycle += value;
					}
				}
				textField.setText(motorcycle);
				if (textField.getText().endsWith(";")) {
					String values = textField.getText().trim();
					values = values.substring(0, values.lastIndexOf(";"));
					System.out.println(values);
				}

			}

			public void mouseReleased(MouseEvent e) {

			}
		});
		// 设置你希望显示多少个选项
		// jlist.setVisibleRowCount(3);
		jlist.setVisible(false);
		panel = new JScrollPane(jlist);
		panel.setBounds(304, 82, 70, 145);
		panel.setVisible(false);
		frame.getContentPane().add(panel);

		frame.getContentPane().add(textField);
		textField.setColumns(10);

		JLabel lblNewLabel = new JLabel("穿过县界长长的隧道");
		lblNewLabel.setBounds(240, 107, 184, 61);
		frame.getContentPane().add(lblNewLabel);

	}
}
